Ext.define('App.inventory.productForm', {
    extend: 'Ext.window.Window',
    alias: 'widget.productForm',
    initComponent: function () {
        var form = Ext.widget('form', {
            frame: false,
            border: true,
            split: true,
            autoScroll: true,
            bodyPadding: 5,
            bodyBorder: true,
            fieldDefaults: {
                labelWidth: 90,
                labelAlign: 'right',
                msgTarget: 'side'
            },
            defaults: {
                anchor: '100%'
            },
            items: [
                {xtype: 'hidden',
                    name: 'id',
                    dataIndex: 'id'
                } ,
                {xtype: 'hidden',
                    name: 'category',
                    dataIndex: 'category'
                },
                {xtype: 'hidden',
                    name: 'deleteFlag',
                    dataIndex: 'deleteFlag'
                },
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [
                    {xtype: 'textfield',
                        name: 'name',
                        dataIndex: 'name',
                        fieldLabel: '产品名称',
                        allowBlank: true
                    }
                    ,
                    {xtype: 'textfield',
                        name: 'number',
                        dataIndex: 'number',
                        fieldLabel: '产品编号',
                        allowBlank: true
                    }
                ]}
                ,
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [
                    Ext.create("comboBoxTree",
                        {
                            name : 'categoryC',
                            storeUrl : getRootPath()+'/codeValueController/treeJsonId.action?idType=treeCode&spareProperty=productCategories',
                            //cascade : 'single',//级联方式:1.child子级联;2.parent,父级联,3,both全部级联
                            checkModel : 'single',//当json数据为不带checked的数据时只配置为single,带checked配置为double为单选,不配置为多选
                            fieldLabel : '产品类别',
                            allowBlank : true,
                            rootId : '', //后天添加数据的时候root不计算在呢
                            rootText : '顶级节点',
                            labelWidth: 90,
                            rootVisible : false,
                            callback:function(id,text){
                                //赋值给隐藏域
                                var form = this.up('productForm').down('form').getForm();
                                form.findField("category").setValue(id);
                            }
                        })
                    ,
                    {xtype: 'textfield',
                        name: 'productType',
                        dataIndex: 'productType',
                        fieldLabel: '型    号',
                        allowBlank: true
                    }
                ]}
                ,
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [
                     new comboBox({
                        name: 'unit',
                        dataIndex: 'unit',
                        fieldLabel: '计量单位',
                        labelAlign: 'right',
                        codeName: '计量单位',
                        queryMode: 'local',
                        typeAhead: false,
                        forceSelection: true,
                        loadingText: '正在搜索...',
                        allowBlank: true,
                        hideTrigger: false
                    })
                    ,
                    {xtype: 'numberfield',
                        name: 'salesPrice',
                        dataIndex: 'salesPrice',
                        allowDecimals: true,
                        decimalPrecision: 2,
                        fieldLabel: '销售价格(元)',
                        allowBlank: true
                    }
                ]}
                ,
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [
                    {xtype: 'numberfield',
                        name: 'lowestPrice',
                        dataIndex: 'lowestPrice',
                        allowDecimals: true,
                        decimalPrecision: 2,
                        fieldLabel: '最低价格(元)',
                        allowBlank: true
                    }
                    ,
                    {xtype: 'numberfield',
                        name: 'costPrice',
                        dataIndex: 'costPrice',
                        allowDecimals: true,
                        decimalPrecision: 2,
                        fieldLabel: '成本价格(元)',
                        allowBlank: true
                    }
                ]}
                ,
                {xtype: 'textfield',
                    name: 'discountsRange',
                    dataIndex: 'discountsRange',
                    fieldLabel: '折扣范围',
                    allowBlank: true
                }
                ,
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [
                    {xtype: 'numberfield',
                        name: 'maximumInventory',
                        dataIndex: 'maximumInventory',
                        allowDecimals: false,
                        decimalPrecision: null,
                        fieldLabel: '最高库存',
                        allowBlank: true
                    }
                    ,
                    {xtype: 'numberfield',
                        name: 'minimumInventory',
                        dataIndex: 'minimumInventory',
                        allowDecimals: false,
                        decimalPrecision: null,
                        fieldLabel: '最低库存',
                        allowBlank: true
                    }
                ]}
                ,
                {
                    xtype: 'container',
                    layout: 'hbox',
                    margin: '0 0 5 0',
                    defaults: {
                        flex: 1
                    }, items: [
                    new comboBox({
                        name: 'usingState',
                        dataIndex: 'usingState',
                        fieldLabel: '状态',
                        labelAlign: 'right',
                        codeName: '是否',
                        queryMode: 'local',
                        typeAhead: false,
                        forceSelection: true,
                        loadingText: '正在搜索...',
                        allowBlank: true,
                        hideTrigger: false
                    })

                    ,
                    {xtype: 'textfield',
                        name: 'barcode',
                        dataIndex: 'barcode',
                        fieldLabel: '国际条码',
                        allowBlank: true
                    }
                ]}
                ,

                    {xtype: 'textarea',
                        name: 'description	',
                        dataIndex: 'description	',
                        fieldLabel: '说明',
                        allowBlank: true
                    }

            ]
        });
        Ext.apply(this, {
            title: '新增',
            layout: 'fit',
            height: 500,
            width: 600,
            modal: true,
            constrain: true,
            closeAction: 'hide',
            buttonAlign: 'center',
            buttons: [
                {
                    text: "保存",
                    handler: function () {
                        //this.up(‘window’)获取了顶级的window组件。接着使用down()方法获取了window的子组件form组件，最后使用getForm()来获取整个form。
                        var form = this.up('productForm').down('form');
                        if (form.getForm().isValid()) {
                            form.getForm().submit({
                                url: getRootPath() + '/productController/save.action',
                                scope: form,
                                success: function (form, action) {
                                    this.up('productForm').close();
                                    Ext.getCmp('productGrid').getStore().reload();
                                },
                                failure: function (form, action) {
                                    if (action.result.message == undefined)
                                        Ext.Msg.alert('错误', '网络连接失败.');
                                    else
                                        Ext.Msg.alert('错误', action.result.message);
                                },
                                waitMsg: '保存数据...'
                            });
                        } else {
                            Ext.Msg.alert('提示', '表单含有无效或空数据');
                        }
                    }
                },
                {
                    text: "关闭",
                    handler: function () {
                        this.up('productForm').close();
                    }
                }
            ],
            items: form
        });

        this.callParent(arguments);
    }
});